const path = require('path');
const objectid = require('objectid');
const db = require('./db');

module.exports = {
    showIndex(req, res) {
        // 从数据库中获取数据
        db.dbConnect((client) => {
            db.dbGetAll(client, (data_obj) => {
                res.render(path.join(__dirname, 'pages', 'index.html'), data_obj);
            });
        });
    },
    showDetails(req, res) {
        let id = req.query.id;
        console.log(id);

        id = new objectid(id);

        db.dbConnect((client) => {

            db.dbGetById(client, id, (data) => {
                res.render(path.join(__dirname, 'pages', 'details.html'), data[0]);
            });
        });
    },
    showSubmit(req, res) {
        res.sendFile(path.join(__dirname, 'pages', 'submit.html'));
    },
    add(req, res) {
        console.log(req.body);
        let data = req.body;

        db.dbConnect((client) => {
            db.dbAdd(client, data, () => {
                res.redirect('/');
            });
        });
    },

    delte(req, res) {
        let id = req.query.id;

        console.log(id);

        id = new objectid(id);

        db.dbConnect((client) => {
            db.dbDelete(client, id, () => {
                res.redirect('/');

            })
        });
    }
}